
/*
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include <stdio.h>
#include <string.h>

#include "libmooigraph.h"
#include "lmg.h"


/* The type of a function used to deallocate any resources associated
   with the key.  */
static void
lmg_uniqslmgsplay_tree_delete_key_fn (lmgsplay_tree_key key)
{
  lmg_free ((void *) key);
  return;
}

char *
lmg_uniqs (char *s)
{
  lmgsplay_tree_node spn = NULL;
  char *news = NULL;
  if (lmgstrings == NULL)
    {
      lmgstrings =
	lmgsplay_tree_new (lmgsplay_tree_compare_strings,
			   lmg_uniqslmgsplay_tree_delete_key_fn, NULL);
    }
  spn = lmgsplay_tree_lookup (lmgstrings, (lmgsplay_tree_key) s);
  if (spn)
    {
      return ((char *) spn->value);
    }
  news = lmg_malloc (strlen (s) + 1);
  (void) strcpy (news, s);
  lmgsplay_tree_insert (lmgstrings, (lmgsplay_tree_key) news,
			(lmgsplay_tree_value) news);
  return (news);
}

void
lmg_uniqsclear (void)
{
  if (lmgstrings)
    {
      lmgsplay_tree_delete (lmgstrings);
      lmgstrings = NULL;
    }
  return;
}

/* End */
